package com.yztstudy.Controller;

import com.yztstudy.Entity.Desk;
import com.yztstudy.Service.DeskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Desk的表示层
 */

@Controller
@Scope("prototype") //spring注解的bean可以变成多例
public class DeskController {

    @Autowired
    private DeskService deskService;

    /**
     * 查询全部
     */
    @GetMapping("/deskQuery")
    public String queryAll(ModelMap map)
    {
        List<Desk> desks = deskService.getDesks("");
        map.addAttribute("desks", desks);
        return "deskList";
    }

    /**
     * 查询全部
     */
    @PostMapping("/deskQuery")
    public String queryByCondition(String keyword, ModelMap map)
    {
        List<Desk> desks = deskService.getDesks(keyword);
        //回显数据
        map.addAttribute("desks", desks);
        map.addAttribute("keyword", keyword);
        return "deskList";
    }

    /**
     * 新增桌子
     */
    @PostMapping("/deskAdd")
    public void addDesk(Desk desk, HttpServletResponse response) throws IOException {

        //字符过滤
        response.setContentType("text/html;charset=utf-8");

        int result = -1;
        try {
            result = deskService.addDesk(desk);
        }catch (Exception e)
        {
            response.getWriter().write("<script>alert('餐桌新增失败!'); location.href = '/page_saveDesk';</script>");
        }finally {
            if (result > 0)
            {
                response.getWriter().write("<script>alert('餐桌新增成功!'); location.href = '/deskQuery';</script>");
            }else {
                response.getWriter().write("<script>alert('餐桌新增失败!'); location.href = '/page_saveDesk';</script>");
            }
        }
    }

    /**
     * 删除桌子
     */
    @RequestMapping("/deskDelete/{id}")
    public void deleteDesk(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException
    {
        //字符过滤
        response.setContentType("text/html;charset=utf-8");

        int result = -1;
        try {
            result = deskService.deleteDesk(id);
        }catch (Exception e)
        {
            response.getWriter().write("<script>alert('删除餐桌失败!'); location.href = '/deskQuery';</script>");
        }finally {
            if (result > 0)
            {
                response.getWriter().write("<script>alert('删除餐桌成功!'); location.href = '/deskQuery';</script>");
            }else {
                response.getWriter().write("<script>alert('删除餐桌失败!'); location.href = '/deskQuery';</script>");
            }
        }
    }

    /**
     * 激活桌子
     */
    @RequestMapping("/deskActive/{id}")
    public void activeDesk(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException
    {

        //字符过滤
        response.setContentType("text/html;charset=utf-8");

        int result = -1;
        try {
            result = deskService.activeDesk(id);
        }catch (Exception e)
        {
            response.getWriter().write("<script>alert('激活餐桌失败!'); location.href = '/deskQuery';</script>");
        }finally {
            if (result > 0)
            {
                response.getWriter().write("<script>alert('激活餐桌成功!'); location.href = '/deskQuery';</script>");
            }else {
                response.getWriter().write("<script>alert('激活餐桌失败!'); location.href = '/deskQuery';</script>");
            }
        }
    }

    /**
     * 预定
     */
    @RequestMapping("/deskOrder/{id}")
    public void orderDesk(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException
    {
        //字符过滤
        response.setContentType("text/html;charset=utf-8");

        int result = -1;
        Desk desk = new Desk();
        desk.setId(id);

        try {
            result = deskService.orderState(desk);
        }catch (Exception e)
        {
            response.getWriter().write("<script>alert('预定餐桌失败!'); location.href = '/deskQuery';</script>");
        }finally {
            if (result > 0)
            {
                response.getWriter().write("<script>alert('预定餐桌成功!'); location.href = '/deskQuery';</script>");
            }else {
                response.getWriter().write("<script>alert('预定餐桌失败!'); location.href = '/deskQuery';</script>");
            }
        }
    }

    /**
     * 取消
     */
    @RequestMapping("/deskCancel/{id}")
    public void cancelDesk(@PathVariable("id") Integer id, HttpServletResponse response) throws IOException
    {
        //字符过滤
        response.setContentType("text/html;charset=utf-8");

        int result = -1;
        Desk desk = new Desk();
        desk.setId(id);

        try {
            result = deskService.cancelState(desk);
        }catch (Exception e)
        {
            response.getWriter().write("<script>alert('退掉餐桌失败!'); location.href = '/deskQuery';</script>");
        }finally {
            if (result > 0)
            {
                response.getWriter().write("<script>alert('退掉餐桌成功!'); location.href = '/deskQuery';</script>");
            }else {
                response.getWriter().write("<script>alert('退掉餐桌失败!'); location.href = '/deskQuery';</script>");
            }
        }
    }

    /**
     * 客户用的功能
     * 获取所有的空闲餐桌,进去前台，点餐
     */
    @GetMapping("/customer")
    public String indexGetDesks(ModelMap map)
    {
        List<Desk> desks = deskService.getFreeDesks();
        map.addAttribute("desks", desks);
        return "customerMain";
    }
}
